package homework.service;

import homework.model.Penguin;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 使用keySet,entrySet遍历集合
 */
public class PenguinManage3 {
    public static void main(String[] args) {

        Penguin p1 = new Penguin("欧欧", "Q仔");
        Penguin p2 = new Penguin("亚亚", "Q妹");
        Penguin p3 = new Penguin("菲菲", "Q妹");
        Penguin p4 = new Penguin("美美", "Q妹");

        Map<String,Penguin> map=new HashMap<>();
        map.put("A",p1);
        map.put("B",p2);
        map.put("C",p3);
        map.put("D",p4);
        
        //使用keySet遍历集合
        Set<String> keySet = map.keySet();
        for (String key:keySet){
            Penguin penguin = map.get(key);
            System.out.println(penguin.getName()+" "+penguin.getGender());
        }

        //使用entrySet遍历集合
        Set<Map.Entry<String, Penguin>> entrySet = map.entrySet();
        Iterator<Map.Entry<String, Penguin>> iterator = entrySet.iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Penguin> entry = iterator.next();
            String key = entry.getKey();
            Penguin p = map.get(key);
            System.out.println(p.getName()+" "+p.getGender());
        }



    }
}
